package com.alan.camunda.boot.delegate;

import com.alan.camunda.boot.util.BpmnUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.camunda.bpm.model.bpmn.instance.FlowElement;
import org.springframework.stereotype.Component;

/**
 * 等待一定的时间，根据节点的扩展字段 seconds 来决定等待的时间
 * 如果未设置则等待 10 秒
 */
@Slf4j
@Component("sleepDelegate")
public class SleepDelegate implements JavaDelegate {


    @Override
    public void execute(DelegateExecution execution) throws Exception {

        FlowElement fe = execution.getBpmnModelElementInstance();
        String seconds = BpmnUtil.getExtensionProperty(fe, "seconds");
        long sleepTime = 10000;
        if (StringUtils.isNotBlank(seconds)) {
            sleepTime = Long.parseLong(seconds) * 1000;
        }
        log.info("环节: {} , 等待时间为: {} ms", execution.getCurrentActivityName(), sleepTime);

        Thread.sleep(sleepTime);


    }



}
